/*
	This file is part of the JKTest
    Copyright (C) 2008 Royce Y. Feng <ryfeng@gmail.com>

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero General Public License version 3
    as published by the Free Software Foundation. You may not use, modify
    or distribute this program under any other version of the
    GNU Affero General Public License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero General Public License for more details.

    You should have received a copy of the GNU Affero General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

package JKDateAndTime;

import javax.swing.JPanel;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JComboBox;
import javax.swing.BorderFactory;
import javax.swing.border.TitledBorder;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;
import javax.swing.BoxLayout;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.GridLayout;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

/**
 * 
 * GUI class used to display the date and time and have the user set the date 
 * and time.
 * 
 * @author Royce Feng
 */
public class DateAndTimeGUI implements ActionListener {

    // Date and Time Handles...
    private DateAndTimeClass DST;
    
    // GUI stuff
    private JDialog window;
    private JPanel panel;
    
    // Current Date and Time
    private JPanel cdate = new JPanel( new GridLayout(1,6) );
    private JPanel ctime = new JPanel( new GridLayout(1,6) );
    private JComboBox cyear = new JComboBox();
    private JComboBox cmonth = new JComboBox();
    private JComboBox cday = new JComboBox();
    private JComboBox chour = new JComboBox();
    private JComboBox cminute = new JComboBox();
    private JComboBox csecond = new JComboBox();
    
    // User set Date and Time
    private JPanel udate = new JPanel( new GridLayout(1,6) );
    private JPanel utime = new JPanel( new GridLayout(1,6) );
    private JComboBox uyear = new JComboBox();
    private JComboBox umonth = new JComboBox();
    private JComboBox uday = new JComboBox();
    private JComboBox uhour = new JComboBox();
    private JComboBox uminute = new JComboBox();
    private JComboBox usecond = new JComboBox();
    
    // Timer to update Time on GUI...
    private Timer updateTime = new Timer();

    private class KeyStalker implements KeyListener {
        public void keyReleased( KeyEvent e ) {}
        public void keyPressed( KeyEvent e ) {
            switch ( e.getKeyCode() ) {
                case 113: //F2
                case 27: // ESC
                    window.setVisible(!window.isVisible());
                    break;
                default:
                    break;
            }
        }
        public void keyTyped( KeyEvent e ) {}
    }
    
    /**
     * 
     * Class that describes the procedure to do on Timer.
     * 
     */
    private class updateTimeTask extends TimerTask {
        public void run() {
            DST.run();
        }
    }
    
    /**
     * 
     * Sets the values for the dropdown boxes
     * 
     */
    public void setDefaultValues() {
        for ( int i = 1970; i < 2050; i++ ) {
            cyear.addItem(i);
            uyear.addItem(i);            
        }
        for ( int i = 1; i < 13; i++ ) {
            cmonth.addItem(i);
            chour.addItem(i);
            umonth.addItem(i);
            uhour.addItem(i);
        }
        for ( int i = 1; i < 32; i++ ) {
            cday.addItem(i);
            uday.addItem(i);
        }
        for ( int i = 0; i < 60; i++ ) {
            cminute.addItem(i);
            csecond.addItem(i);
            uminute.addItem(i);
            usecond.addItem(i);            
        }
    }
    
    /**
     * 
     * Main Constructor for DateAndTimeGUI(). Creates JLabels and JComboBoxes that
     * correspond the year/month/day/hour/minute/second of the current time and
     * that of the user set time.
     * 
     */
    public DateAndTimeGUI() {
        window = new JDialog();
        window.addKeyListener(new KeyStalker());
        panel = new JPanel();
        panel.setLayout(new BoxLayout(panel,BoxLayout.Y_AXIS));
        setDefaultValues();
        panel.add(cdate);
        panel.add(ctime);
        panel.add(udate);
        panel.add(utime);
        TitledBorder title = BorderFactory.createTitledBorder("Current Date");
        cdate.setBorder(title);
        title = BorderFactory.createTitledBorder("Current Time");
        ctime.setBorder(title);
        title = BorderFactory.createTitledBorder("Set Date:");
        udate.setBorder(title);
        title = BorderFactory.createTitledBorder("Set Time:");
        utime.setBorder(title);
        //cdate.setPreferredSize(new Dimension(580,70));
        //ctime.setPreferredSize(new Dimension(580,70));
        //udate.setPreferredSize(new Dimension(580,70));
        //utime.setPreferredSize(new Dimension(580,70));
        cyear.addKeyListener(new KeyStalker());
        cmonth.addKeyListener(new KeyStalker());
        cday.addKeyListener(new KeyStalker());
        chour.addKeyListener(new KeyStalker());
        cminute.addKeyListener(new KeyStalker());
        csecond.addKeyListener(new KeyStalker());
        uyear.addKeyListener(new KeyStalker());
        umonth.addKeyListener(new KeyStalker());
        uday.addKeyListener(new KeyStalker());
        uhour.addKeyListener(new KeyStalker());
        uminute.addKeyListener(new KeyStalker());
        usecond.addKeyListener(new KeyStalker());
        cdate.add(new JLabel("Year:"));
        cdate.add(cyear);
        cdate.add(new JLabel("Month:"));
        cdate.add(cmonth);
        cdate.add(new JLabel("Day:"));
        cdate.add(cday);
        ctime.add(new JLabel("Hour:"));
        ctime.add(chour);
        ctime.add(new JLabel("Minute:"));
        ctime.add(cminute);
        ctime.add(new JLabel("Second:"));
        ctime.add(csecond);
        udate.add(new JLabel("Year:"));
        udate.add(uyear);
        udate.add(new JLabel("Month:"));
        udate.add(umonth);
        udate.add(new JLabel("Day:"));
        udate.add(uday);
        utime.add(new JLabel("Hour:"));
        utime.add(uhour);
        utime.add(new JLabel("Minute:"));
        utime.add(uminute);
        utime.add(new JLabel("Second:"));
        utime.add(usecond);
        JButton setTimeButton = new JButton("Set Date and Time");
        setTimeButton.setPreferredSize(new Dimension(150,25));
        setTimeButton.setActionCommand("SDaT");
        setTimeButton.addActionListener(this);
        panel.add(setTimeButton);
        window.setContentPane(panel);
        window.setResizable ( false );
        window.setTitle("JKTest - Date and Time");
        DST = new DateAndTimeClass(this);
        window.setVisible ( false );
        window.setDefaultCloseOperation ( JDialog.HIDE_ON_CLOSE );
        updateTime.scheduleAtFixedRate(new updateTimeTask(),0,1000);
        window.pack();
    }
    
    /**
     * 
     * Function to set the date and time on button click.
     * 
     * @param e The ActionEvent
     */
    public void actionPerformed(ActionEvent e) {
        if ("SDaT".equals(e.getActionCommand())) {
            int y = uyear.getSelectedIndex() + 1970;
            int m = umonth.getSelectedIndex()+1;
            int d = uday.getSelectedIndex()+1;
            int hour = uhour.getSelectedIndex()+1;
            int minute = uminute.getSelectedIndex();
            int second = usecond.getSelectedIndex();
            if ( DateAndTimeClass.setDateAndTime(y, m, d, hour, minute, second) ) {
                JOptionPane.showMessageDialog(window, "Date and Time were successfully set");
            } else {
                JOptionPane.showMessageDialog(window, "Date and Time were not successfully set. " +
                        "Please do it manually.");
            }
        }
    }

    /**
     *
     * Swaps the current visibility (if true, then false, if false then true)
     *
     */
    public void toggleVisible() {
        window.setVisible(!window.isVisible());
    }

    /**
     * 
     * Sets the JComboBoxes based on parameters
     * 
     * @param y Year
     * @param m Month
     * @param d Day
     * @param hour Hour
     * @param min Minute
     * @param sec Second
     */
    public void setTime( int y, int m, int d, int hour, int min, int sec ) {
        int mon = m + 1;
        if ( mon > 12 ) {
            mon = 1;
        }
        this.cyear.setSelectedItem(y);
        this.cmonth.setSelectedItem(mon);
        this.cday.setSelectedItem(d);
        this.chour.setSelectedItem(hour);
        this.cminute.setSelectedItem(min);
        this.csecond.setSelectedItem(sec);
    }

    /**
     *
     * Reskins and packs the GUI
     *
     */
    public void pack() {
        SwingUtilities.updateComponentTreeUI(window);
        window.pack();
    }
    
}
